package LeetCode;

/**
 * @Author Kechelle
 * @Date 2020/9/22 11:05
 * @Version 1.0
 */
public class Code14 {
    public static String longestCommonPrefix(String[] strs){
        if (strs.length == 0 || strs == null){
            return "";
        }
        //以第一个字符串的各字符开始遍历
        for (int i=0;i<strs[0].length();i++){
            char c = strs[0].charAt(i);
            for (int j=1;j< strs.length;j++){
                //当遇到不相等的字符或者有字符串长度比第一个字符串短时推出循环
                if (i==strs[j].length() || strs[j].charAt(i)!=c){
                    return strs[0].substring(0,i);
                }
            }
        }
        return strs[0];
    }

    public static void main(String[] args) {
        String[] strs = {"aa","a"};
        System.out.println(longestCommonPrefix(strs));
    }
}
